package com.ruoyi.web.controller.edh;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.edh.domain.EdhMicroshopGoods;
import com.ruoyi.edh.domain.EdhShoppingCart;
import com.ruoyi.edh.domain.model.EdhMicroshopGoodsModel;
import com.ruoyi.edh.service.IEdhMicroshopGoodsService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * 易订货-微店上架商品Controller
 *
 * @author xxx
 * @date 2020-09-02
 */
@RestController
@RequestMapping("/edh/smallgoods")
@Api(description = "易订货微店商品管理")
public class EdhMicroshopGoodsController extends BaseController {
    @Autowired
    private IEdhMicroshopGoodsService edhMicroshopGoodsService;

    /**
     * 我的微店的商品列表
     */
    @GetMapping("/list")
    @ApiOperation("我的微店的商品列表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "title", value = "商品名称"),
            @ApiImplicitParam(name = "sortType", value = "排序类别 1奖励最高 2最新发布 3价格升序 4价格降序")
    })
    public TableDataInfo list(String title, Integer sortType) {
        startPage();
        List<Map<String, Object>> list = edhMicroshopGoodsService.selectEdhMicroshopGoodsList(title, sortType);
        return getDataTable(list);
    }

    /**
     * 平台查询团长的商品列表
     */
    @GetMapping("/getList")
    @ApiOperation("平台查询团长的商品列表")
    public TableDataInfo getList(Long userId, String title, String onSale) {
        startPage();
        List<Map<String, Object>> list = edhMicroshopGoodsService.getList(userId, title, onSale);
        return getDataTable(list);
    }


    /**
     * 查询微店可以上架的商品
     */
    @GetMapping("/reselllist")
    @ApiOperation("查询微店可以上架的商品")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "title", value = "商品名称"),
            @ApiImplicitParam(name = "sortType", value = "排序类别 1奖励最高 2最新发布 3价格升序 4价格降序")
    })
    public TableDataInfo reselllist(String title, Integer sortType) {
        startPage();
        List<Map<String, Object>> list = edhMicroshopGoodsService.reselllist(title, sortType);
        return getDataTable(list);
    }

    /**
     * 微店一键上架商品
     */
    @GetMapping("/onSale")
    @ApiOperation("微店一键上架商品")
    @ApiImplicitParam(name = "goods", value = "商品id数组")
    public AjaxResult onSale(@RequestParam(value = "goods") List<Long> goods) {
        edhMicroshopGoodsService.onSale(goods);
        return AjaxResult.success("上架成功");
    }

    /**
     * 微店商品全部上架
     */
    @GetMapping("/allOnSale")
    @ApiOperation("微店商品全部上架")
    public AjaxResult allOnSale() {
        edhMicroshopGoodsService.allOnSale();
        return AjaxResult.success("上架成功");
    }

    /**
     * 微店商品上下架/置顶
     */
    @Log(title = "易订货-微店上架商品", businessType = BusinessType.UPDATE)
    @PostMapping
    @ApiOperation("微店商品上下架/置顶")
    public AjaxResult edit(@Validated @RequestBody EdhMicroshopGoodsModel edhMicroshopGoods) {
        EdhMicroshopGoods update = new EdhMicroshopGoods();
        update.setId(edhMicroshopGoods.getId());
        update.setOnSale(edhMicroshopGoods.getOnSale());
        update.setSort(edhMicroshopGoods.getSort());
        if (StringUtils.isNotNull(edhMicroshopGoods.getOnSale()) && 1 == edhMicroshopGoods.getOnSale()) {
            EdhShoppingCart.killGoodsBymircoGoodsId(edhMicroshopGoods.getId());
        }
        return toAjax(edhMicroshopGoodsService.updateEdhMicroshopGoods(update));
    }

    /**
     * 删除微店中的商品id
     */
    @Log(title = "删除微店商品", businessType = BusinessType.DELETE)
    @GetMapping("/deleteGoods")
    @ApiOperation("删除微店商品")
    public AjaxResult remove(Long goodsId) {
        edhMicroshopGoodsService.deleteEdhMicroshopGoodsByMshopIdAndGodosId(goodsId);
        return AjaxResult.success();
    }

    /**
     * 删除微店商品
     */
    @Log(title = "删除微店商品", businessType = BusinessType.DELETE)
    @DeleteMapping
    @ApiOperation("删除微店商品")
    public AjaxResult remove(Long[] ids) {
        return toAjax(edhMicroshopGoodsService.deleteEdhMicroshopGoodsByIds(ids));
    }

}
